package com.lvtongka.app.fragment.my.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.lvtongka.app.common.bean.IconContainer
import com.lvtongka.app.databinding.GridItemLayoutBinding

class MyGridAdapter(val context: Context, val list: List<IconContainer>) : BaseAdapter() {

    override fun getCount(): Int {
        return list.size
    }

    override fun getItem(position: Int): IconContainer {
        return list[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var holder : MyGridViewHolder
        var finalView = convertView
        if (finalView == null) {
            var binding = GridItemLayoutBinding.inflate(LayoutInflater.from(context), parent, false)
            finalView = binding.root
            holder = MyGridViewHolder(binding)
            finalView.tag = holder
        } else {
            holder = finalView.tag as MyGridViewHolder
        }
        var ic = list[position]
        ic?.let {
            holder.binding.ivMyIcon.setImageResource(it.imgId)
            holder.binding.tvMyName.text = it.name
        }
        return finalView!!
    }

    open inner class MyGridViewHolder(var binding: GridItemLayoutBinding)
}